package com.elitel.frame.base.service.impl;

import com.elitel.frame.base.component.exportprojectdata.DataBaseConnInfo;
import com.elitel.frame.base.service.SysFunctionService;
import com.elitel.frame.main.dao.SysFunctionMapper;
import com.elitel.frame.main.dao.ext.ExtSysFunctionMapper;
import com.elitel.frame.main.entity.DataSourceType;
import com.elitel.frame.main.entity.SysFunction;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.ext.SysFunctionExt;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class SysFunctionServiceImpl implements SysFunctionService {

    private static final Logger logger = LoggerFactory.getLogger(SysFunctionServiceImpl.class);

    @Autowired
    private SysFunctionMapper sysFunctionMapper;
    @Autowired
    private ExtSysFunctionMapper extSysFunctionMapper;
    @Autowired
    private DataBaseConnInfo dataBaseConnInfo;

    @Override
    public PageResponseVo<SysFunctionExt> getSysFuncByPagination(PageRequestVo<SysPrjservice> pr) {
        String databaseType = dataBaseConnInfo.dataBaseType;
        List<SysFunctionExt> list = new ArrayList<SysFunctionExt>();
        PageResponseVo<SysFunctionExt> responseVo = new PageResponseVo<SysFunctionExt>();

        switch (databaseType) {
            case "oracle":
            case DataSourceType.DM:
                // TODO 适配达梦,待测试,接口对应的功能未启用
                pr.StartPage();
                list = extSysFunctionMapper.getSysFunctionByPaginationOracle(pr);
                break;
            case "mysql":
            case "sqlite":
            case "h2":
                pr.setPageNo((pr.getPageNo() - 1) * pr.getPageSize());
                list = extSysFunctionMapper.getSysFunctionByPaginationMysql(pr);
                break;
            case DataSourceType.POSTGRES:
                pr.setPageNo((pr.getPageNo() - 1) * pr.getPageSize());
                list = extSysFunctionMapper.getSysFunctionByPaginationPG(pr);
                break;
        }

        Long count = extSysFunctionMapper.getSysFunctionCount(pr);
        responseVo.setRows(list);
        responseVo.setTotal(count.intValue());
        return responseVo;
    }

    @Override
    public Integer insertSysFunction(SysFunction sysFunction) {
        sysFunction.setGuid(UUID.randomUUID().toString());
        return sysFunctionMapper.insertSelective(sysFunction);
    }

    @Override
    public SysFunction getSysFunctionByGuid(String guid) {
        return sysFunctionMapper.selectByPrimaryKey(guid);
    }

    @Override
    public Integer updateSysFunction(SysFunction sysFunction) {
        return sysFunctionMapper.updateByPrimaryKeySelective(sysFunction);
    }

    @Override
    public Integer deleteSysFunctionByGuids(String guids) {
        List<String> list = new ArrayList<String>();
        String[] idArray = guids.split(",");
        for (int i = 0; i < idArray.length; i++) {
            list.add(idArray[i]);
        }
        return extSysFunctionMapper.deleteSysFunctionByGuids(list);
    }

    @Override
    public List<SysFunctionExt> getSysFunctionByPcode(String pcode) {
        List<SysFunctionExt> list = extSysFunctionMapper.getSysFunctionByPcode(pcode);
        if (list == null || list.isEmpty())
            return list;
        for (SysFunctionExt sysfunc : list) {
            String methodname = StringUtils.substringBetween(sysfunc.getFuncscript(), "function", "{").trim();
            sysfunc.setMethodname(methodname);
        }
        return list;
    }
}
